/*
 *
 * COM232 - Minera��o de Dados e Intelig�ncia de Neg�cios - 2009/1
 * Prof. Dr. Ahmed Ali Abdalla Esmin
 *
 *
 */

package main;

import main.pso.PSO;
import java.awt.FlowLayout;
import java.text.DecimalFormat;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import main.pso.HPSOM;
import main.pso.HPSOMKMeans;
import main.pso.PSOKMeans;
import main.pso.SimpleKMeans;
import main.pso.StandartPSO;

/**
 *
 * @author Gustavo Sousa
 */
public class PSOFrame extends javax.swing.JFrame {

    /** Creates new form KNNFrame */
    public PSOFrame() {
        initComponents();
        mutacaoLabel.setVisible(false);
        mutacaoText.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgInputType = new javax.swing.ButtonGroup();
        bgOutputType = new javax.swing.ButtonGroup();
        jpMenu = new javax.swing.JPanel();
        jlInputFile = new javax.swing.JLabel();
        jtfFilePath = new javax.swing.JTextField();
        jbFileChoose = new javax.swing.JButton();
        jlK = new javax.swing.JLabel();
        jlParameters = new javax.swing.JLabel();
        jlObservationK = new javax.swing.JLabel();
        jlNumberOfParticles = new javax.swing.JLabel();
        jlNumberOfIterations = new javax.swing.JLabel();
        jlW = new javax.swing.JLabel();
        jtfWmax = new javax.swing.JTextField();
        jlC1 = new javax.swing.JLabel();
        jtfC1 = new javax.swing.JTextField();
        jlObservationC1 = new javax.swing.JLabel();
        jlC2 = new javax.swing.JLabel();
        jtfC2 = new javax.swing.JTextField();
        jlObservationC2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jtfWmin = new javax.swing.JTextField();
        jTipo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        mutacaoLabel = new javax.swing.JLabel();
        mutacaoText = new javax.swing.JTextField();
        jsNumberOfParticles = new javax.swing.JTextField();
        jsNumberOfIterations = new javax.swing.JTextField();
        jsK = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jNumberTimes = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jbAbout = new javax.swing.JButton();
        jbExecute = new javax.swing.JButton();
        jlResults = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaResults = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PSO usado para a clusterizacao de dados");
        setResizable(false);

        jpMenu.setBackground(new java.awt.Color(255, 255, 255));
        jpMenu.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jlInputFile.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlInputFile.setText("Arquivo de Entrada:");

        jtfFilePath.setEditable(false);
        jtfFilePath.setText("C:\\\\Documents and Settings\\\\Amigao\\\\Desktop\\\\Gustavo\\\\workspace\\\\psoclustering\\\\src\\\\main\\\\iris_data.data");
        jtfFilePath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfFilePathActionPerformed(evt);
            }
        });

        jbFileChoose.setText("...");
        jbFileChoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbFileChooseActionPerformed(evt);
            }
        });

        jlK.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlK.setText("K:");

        jlParameters.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlParameters.setText("Parâmetros:");

        jlObservationK.setFont(new java.awt.Font("Tahoma", 0, 10));
        jlObservationK.setText("*Representa o número de clusters a serem encontrados.");

        jlNumberOfParticles.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlNumberOfParticles.setText("Número de Partículas:");

        jlNumberOfIterations.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlNumberOfIterations.setText("Número de Iterações:");

        jlW.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlW.setText("W - máx:");

        jtfWmax.setText("0.17");
        jtfWmax.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfWmaxActionPerformed(evt);
            }
        });

        jlC1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlC1.setText("C1:");

        jtfC1.setText("0.2");
        jtfC1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfC1ActionPerformed(evt);
            }
        });

        jlObservationC1.setFont(new java.awt.Font("Tahoma", 0, 10));
        jlObservationC1.setText("* Representa o peso da influência da memória da partícula em seu movimento.");

        jlC2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlC2.setText("C2:");

        jtfC2.setText("0.2");
        jtfC2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfC2ActionPerformed(evt);
            }
        });

        jlObservationC2.setFont(new java.awt.Font("Tahoma", 0, 10));
        jlObservationC2.setText("*Representa o peso da influência da melhor particula no movimento das outras.");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("W - min:");

        jtfWmin.setText("0.02");

        jTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Standart PSO", "K-Means PSO", "Simple K-Means", "HPSOM","HPSOM KMeans" }));
        jTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTipoActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("tipo:");

        mutacaoLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        mutacaoLabel.setText("% mutação:");

        mutacaoText.setText("10");
        mutacaoText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mutacaoTextActionPerformed(evt);
            }
        });

        jsNumberOfParticles.setText("20");
        jsNumberOfParticles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jsNumberOfParticlesActionPerformed(evt);
            }
        });

        jsNumberOfIterations.setText("200");

        jsK.setText("3");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("rodar");

        jNumberTimes.setText("1");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("vezes");

        javax.swing.GroupLayout jpMenuLayout = new javax.swing.GroupLayout(jpMenu);
        jpMenu.setLayout(jpMenuLayout);
        jpMenuLayout.setHorizontalGroup(
            jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMenuLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpMenuLayout.createSequentialGroup()
                        .addComponent(jlInputFile)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jbFileChoose, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpMenuLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jlParameters)
                        .addGap(18, 18, 18)
                        .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpMenuLayout.createSequentialGroup()
                                .addComponent(jlNumberOfParticles)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jsNumberOfParticles, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jlNumberOfIterations)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jsNumberOfIterations, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpMenuLayout.createSequentialGroup()
                                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jlC2)
                                    .addComponent(jlC1)
                                    .addComponent(jlW)
                                    .addComponent(jlK))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jpMenuLayout.createSequentialGroup()
                                        .addComponent(jTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(mutacaoLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(mutacaoText, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jpMenuLayout.createSequentialGroup()
                                        .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jtfWmax, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jtfC2, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jtfC1, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jsK))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jlObservationC2)
                                            .addComponent(jlObservationK)
                                            .addComponent(jlObservationC1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(jpMenuLayout.createSequentialGroup()
                                                .addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jtfWmin, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(jpMenuLayout.createSequentialGroup()
                                        .addComponent(jNumberTimes, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4)))))))
                .addGap(33, 33, 33))
        );
        jpMenuLayout.setVerticalGroup(
            jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMenuLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlInputFile)
                    .addComponent(jtfFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbFileChoose))
                .addGap(26, 26, 26)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlNumberOfParticles)
                    .addComponent(jlNumberOfIterations)
                    .addComponent(jsNumberOfParticles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jsNumberOfIterations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlParameters))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlObservationK)
                    .addComponent(jsK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlK))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfWmax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jtfWmin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlW))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfC1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlObservationC1)
                    .addComponent(jlC1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfC2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlObservationC2)
                    .addComponent(jlC2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(mutacaoLabel)
                    .addComponent(mutacaoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jNumberTimes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jbAbout.setText("Sobre");
        jbAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAboutActionPerformed(evt);
            }
        });

        jbExecute.setText("Executar");
        jbExecute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExecuteActionPerformed(evt);
            }
        });

        jlResults.setFont(new java.awt.Font("Tahoma", 1, 14));
        jlResults.setText("Resultados:");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jtaResults.setColumns(20);
        jtaResults.setRows(5);
        jScrollPane1.setViewportView(jtaResults);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jpMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 626, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jbExecute)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 481, Short.MAX_VALUE)
                        .addComponent(jbAbout, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jlResults, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbExecute)
                    .addComponent(jbAbout))
                .addGap(21, 21, 21)
                .addComponent(jlResults)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbFileChooseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbFileChooseActionPerformed
        JFileChooser fc = new JFileChooser();

        if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }

        jtfFilePath.setText (fc.getSelectedFile().getPath());
    }//GEN-LAST:event_jbFileChooseActionPerformed

    private void jbExecuteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExecuteActionPerformed

       
        
        try {

            int k = Integer.valueOf(jsK.getText());
            int numberOfParticles = Integer.valueOf(jsNumberOfParticles.getText());
            int numberOfIterations = Integer.valueOf(jsNumberOfIterations.getText());
            double w = Double.valueOf(jtfWmax.getText());
            double w2 = Double.valueOf(jtfWmin.getText());
            double c1 = Double.valueOf(jtfC1.getText());
            double c2 = Double.valueOf(jtfC2.getText());
            String tipo = jTipo.getSelectedItem().toString();
            
            String filePath = jtfFilePath.getText();
            StringBuilder output = new StringBuilder("");
            output.append("\n Erros nas Execucoes:\n");
            PSO pso = new StandartPSO(filePath);
            int numberTimes = Integer.parseInt(jNumberTimes.getText());
            Double[] errors = new Double[numberTimes];
            Double[] squaredErors = new Double[numberTimes];
            Double totalErro = 0.0;
            Double totalSquaredErro = 0.0;
            for(int i = 0; i< numberTimes; i++){
            	if(tipo.equals("Standart PSO")){
                    pso = new StandartPSO(filePath);
                }else if(tipo.equals("Simple K-Means")){
                    pso= new SimpleKMeans(filePath);
                }else if(tipo.equals("HPSOM")){
                    pso= new HPSOM(filePath,Integer.parseInt(mutacaoText.getText()));
                }else if(tipo.equals("HPSOM KMeans")){
                    pso= new HPSOMKMeans(filePath,Integer.parseInt(mutacaoText.getText()));
                }else{
                    pso = new PSOKMeans(filePath);
                }
                pso.setC2(c2);
                pso.setC1(c1);
                pso.setwMax(w);
                pso.setwMin(w2);
            	pso.execute(k,numberOfParticles, numberOfIterations);
            	errors[i] = pso.getErrorTotal();
            	squaredErors[i] = pso.getGlobalBestPosition().getFitness();
            	output.append((i+1)+" - "+errors[i]+"  ---- "+squaredErors[i]+" \n ");
            	totalErro += errors[i];
            	totalSquaredErro += squaredErors[i];
            }
            output.append("\n Media total:"+(totalErro/numberTimes)+"   ---   "+(totalSquaredErro/numberTimes));
            pso.showChartClassification();

            output.append("\n"+pso.getError());
            
            jtaResults.setText(output.toString());

        }catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                "Ocorreu um erro! Verifique se todos os dados foram passados corretamente.",
                "Sobre...", JOptionPane.ERROR_MESSAGE);

        }
    }//GEN-LAST:event_jbExecuteActionPerformed

    private void jbAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAboutActionPerformed

        JOptionPane.showMessageDialog(this,
                "Universidade Federal de Lavras" +
                "\nBacharelado de Ciência da Computação" +
                "\n\nCOM232 - Mineração de Dados e Inteligência de Negócios - 2009/1" +
                "\nTrabalho: Implementação de uma série de algoritmos PSO para Clusterização." +
                "\nAutor:   Gustavo Sousa\n\n",
                "Sobre...", JOptionPane.INFORMATION_MESSAGE);

    }//GEN-LAST:event_jbAboutActionPerformed

    private void jtfWmaxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfWmaxActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jtfWmaxActionPerformed

    private void jtfC1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfC1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jtfC1ActionPerformed

    private void jtfC2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfC2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfC2ActionPerformed

    private void jtfFilePathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfFilePathActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfFilePathActionPerformed

    private void jTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTipoActionPerformed
        if((jTipo.getSelectedItem() != "HPSOM")&&(jTipo.getSelectedItem() != "HPSOM KMeans")){
            mutacaoLabel.setVisible(false);
            mutacaoText.setVisible(false);
        }else{
            mutacaoLabel.setVisible(true);
            mutacaoText.setVisible(true);
        }
    }//GEN-LAST:event_jTipoActionPerformed

    private void mutacaoTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mutacaoTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_mutacaoTextActionPerformed

    private void jsNumberOfParticlesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jsNumberOfParticlesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jsNumberOfParticlesActionPerformed

    private boolean validateFields() {
        return true;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PSOFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgInputType;
    private javax.swing.ButtonGroup bgOutputType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jNumberTimes;
    private javax.swing.JComboBox jTipo;
    private javax.swing.JButton jbAbout;
    private javax.swing.JButton jbExecute;
    private javax.swing.JButton jbFileChoose;
    private javax.swing.JLabel jlC1;
    private javax.swing.JLabel jlC2;
    private javax.swing.JLabel jlInputFile;
    private javax.swing.JLabel jlK;
    private javax.swing.JLabel jlNumberOfIterations;
    private javax.swing.JLabel jlNumberOfParticles;
    private javax.swing.JLabel jlObservationC1;
    private javax.swing.JLabel jlObservationC2;
    private javax.swing.JLabel jlObservationK;
    private javax.swing.JLabel jlParameters;
    private javax.swing.JLabel jlResults;
    private javax.swing.JLabel jlW;
    private javax.swing.JPanel jpMenu;
    private javax.swing.JTextField jsK;
    private javax.swing.JTextField jsNumberOfIterations;
    private javax.swing.JTextField jsNumberOfParticles;
    private javax.swing.JTextArea jtaResults;
    private javax.swing.JTextField jtfC1;
    private javax.swing.JTextField jtfC2;
    private javax.swing.JTextField jtfFilePath;
    private javax.swing.JTextField jtfWmax;
    private javax.swing.JTextField jtfWmin;
    private javax.swing.JLabel mutacaoLabel;
    private javax.swing.JTextField mutacaoText;
    // End of variables declaration//GEN-END:variables

}
